//
//  WBMainViewController.swift
//  陌路微博
//
//  Created by 张继强 on 16/12/2.
//  Copyright © 2016年 张继强. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setChildController()
        self.setComposeButton()

    }
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait//竖屏
//        return .landscape//横屏
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
//    私有控件，撰写按钮
    lazy var composeButtton:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    //FIXME:没有实现
//    objec允许该方法在运行机制中使用oc的消息运行时机制进行调用
     @objc fileprivate func composeAction(){
        print("撰写按钮的实现")
        let vc = UIViewController()
        let nav = UINavigationController.init(rootViewController: vc)
        present(nav, animated: true, completion: nil)
    }
}
// MARK:扩展类目方法
extension WBMainViewController{
    fileprivate func setComposeButton(){
      self.tabBar.addSubview(self.composeButtton)
        let iteamWidth = CGFloat(self.view.bounds.size.width)/CGFloat((self.viewControllers?.count)!)-1
//        self.composeButtton.bounds.insetBy(dx: 2*iteamWidth, dy: 0)
        self.composeButtton.frame = self.tabBar.bounds.insetBy(dx: 2*iteamWidth, dy: 0)
        self.composeButtton.addTarget(self, action: #selector(composeAction), for: .touchUpInside)
    }
    //MARK:设置所有的子Controller
    fileprivate func setChildController(){
        let arrays = [["clsName":"WBHomeController","title":"首页","imageName":"home"],
                      ["clsName":"WBMessageController","title":"消息","imageName":"message_center"],
                      ["claName":"UIViewController"],
                      ["clsName":"WBDiscoverController","title":"发现","imageName":"discover"],
                      ["clsName":"WBProfileController","title":"我的","imageName":"profile"]]
        var ctrolArray = [UIViewController]()
        for dict in arrays {
           ctrolArray.append(self.controller(dict: dict))
        }
        self.viewControllers = ctrolArray
    }
    //将字典建立成相应的Controller
//    paramsDic = [clsName,title,imageName]
    fileprivate  func controller(dict:[String:String])->UIViewController{
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName)as? UIViewController.Type
        else{
          return UIViewController()
        }
        //创建试图控制器
//        1.将clsName转换成cls
        let vc = cls.init()
        vc.title = title
        //设置按钮图片
        vc.tabBarItem.image = UIImage.init(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage.init(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //设置字体
//        通过标题属性字典调整字体颜色和大小
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        //系统默认字体的大小是12号字
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: .normal)
        let nav = WBNavigationController.init(rootViewController: vc)
        return nav
    }
}
